﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Transport.Core.Models.Parcels;
using Transport.Infrastructure.Mvc;
using Transport.Models;
using Transport.Services;
using Transport.Services.Infrastructure;
using Transport.Services.Parcels;
using Transport.Services.Settings;

namespace Transport.Controllers
{
    public class TrackingController : ControllerBase<ITrackingRepository, Tracking>
    {
        private readonly IParcelRepository _parcelRepository;
        private readonly IStateProvinceRepository _stateProvinceRepository;

        public TrackingController(IUnitOfWork unitOfWork, ITrackingRepository repository,
                                IParcelRepository parcelRepository, IStateProvinceRepository stateProvinceRepository)
            : base(repository, unitOfWork)
        {
            _parcelRepository = parcelRepository;
            _stateProvinceRepository = stateProvinceRepository;
        }

        //
        // GET: /Tracing/

        public ActionResult Index(string TrackingNumbers)
        {
            var model = new ListTracingModel
                            {
                                TrackingNumbers = TrackingNumbers, 
                                Trackings = new List<TracingDetailModel>()
                            };
            if (!string.IsNullOrEmpty(TrackingNumbers))
            {
                var listTracking = TrackingNumbers.Split(',');
                //Get maximum 5 item
                if(listTracking.Count()>5)
                    listTracking = listTracking.Take(5).ToArray();
                for (var i = 0; i < listTracking.Count(); i++)
                {
                    listTracking[i] = string.IsNullOrEmpty(listTracking[i]) ? string.Empty : listTracking[i].Replace(" ", "");
                }
                model.Trackings =
                    (from x in _parcelRepository.Tracing(listTracking).Include(
                             p => p.Trackings.Select(y => y.StateProvince))
                     join s in _stateProvinceRepository.GetAll() on x.DeliverToId equals s.StateProvinceId
                    
                
                     select new TracingDetailModel
                                {
                                    DateOfSending = x.DateOfSending,
                                    ReceivedDate = x.ReceivedDate,
                                    Receiver = x.Receiver,
                                    Status = x.Status,
                                    TrackingNumber = x.TrackingNumbers,
                                    Trackings = x.Trackings.Select(p => new TrackingModel
                                                                            {
                                                                                DateOfSending = p.DateOfSending,
                                                                                Status = p.Status,
                                                                                PostOfficeName = p.StateProvince.StateProvinceName,
                                                                                DeliverToName = s.StateProvinceName,
                                                                            })
                                }).ToList();
            }
            return View(model);
        }

    }
}
